﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.advert
{
    public partial class ad_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.advert().Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.advert bll = new BLL.advert();
            Model.advert model = bll.GetModel(_id);

            if (model.status == 1)
            {
                cbStatus.Checked = false;
            }
            else
            {
                cbStatus.Checked = true;
            }
            ddlCategoryID.SelectedValue = model.category_id.ToString();
            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            txtSortId.Text = model.sort_id.ToString();

            if (model.company_id > 0)
            {
                lbCompanyName.Text = new BLL.company().GetTitle(model.company_id);
            }
            txtCompanyID.Text = model.company_id.ToString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.advert model = new Model.advert();
                BLL.advert bll = new BLL.advert();

                model.category_id = int.Parse(ddlCategoryID.SelectedValue);
                model.title = txtTitle.Text.Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                if (cbStatus.Checked == true)
                {
                    model.status = 0;
                }
                else
                {
                    model.status = 1;
                }
                model.add_time = DateTime.Now;
                model.company_id = Utils.StrToInt(txtCompanyID.Text.Trim(), 0);

                if (bll.Add(model) > 0)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.advert bll = new BLL.advert();
                Model.advert model = bll.GetModel(_id);

                model.category_id = int.Parse(ddlCategoryID.SelectedValue);
                model.title = txtTitle.Text.Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.sort_id = int.Parse(txtSortId.Text.Trim());
                if (cbStatus.Checked == true)
                {
                    model.status = 0;
                }
                else
                {
                    model.status = 1;
                }
                model.company_id = Utils.StrToInt(txtCompanyID.Text.Trim(), 0);

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改广告成功！", "ad_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加广告成功！", "ad_list.aspx");
            }
        }
    }
}